package com.example.demo.handler.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

@Slf4j
public class CompanyParser extends AnalysisEventListener<Map<Integer, String>> {

    private final Company company = new Company();
    private Company.Menu menu;

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        super.invokeHeadMap(headMap, context);
        log.info("开始解析头部");
        menu = new Company.Menu();
        menu.name = context.readSheetHolder().getSheetName();
        for (Map.Entry<Integer, String> entry : headMap.entrySet()) {
            Company.Column column = new Company.Column();
            column.name = entry.getValue();
            menu.columns.add(column);
        }
        company.menus.add(menu);
    }

    @Override
    public void invoke(Map<Integer,String> data, AnalysisContext context) {
        log.info(data.toString());
        if (menu != null) {
            Company.Content content = new Company.Content();
            for (Map.Entry<Integer, String> entry : data.entrySet()) {
                content.values.add(entry.getValue());
            }
            menu.contents.add(content);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        log.info("解析结束 ");
        menu = null;
    }

    public Company getCompany() {
        return company;
    }
}
